﻿using System;
using System.Collections.Generic;
using GameEngine.Model;
using GameEngine.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.Model.Data;

namespace GameEngine.View
{
    public class LevelMediatorConstants<MyIdType>
        where MyIdType : IComparable<MyIdType>
    {
        public static string GetMediatorNameFromProxy(ILevelDataProxy<MyIdType> proxy)
        {
            return proxy.LevelName + "_LevelMediator";
        }
    }

    public class LevelMediator<MyIdType, MyBaseUIElementType> : Mediator
        where MyIdType : IComparable<MyIdType>
    {
        //public const string ADDITEMTOMAP_NOTIFICATION = "MapMediator_AddItem";
        //public const string REMOVEITEMFROMMAP_NOTIFICATION = "MapMediator_RemoveItem";
        //public const string MOUSEONITEMS_NOTIFICATION = "MapMediator_RemoveItem";

        public readonly ILevelDataProxy<MyIdType> proxy;
        public readonly IDictionary<IWorldVOId,WorldMediator<MyIdType, MyBaseUIElementType>> worldMediators;

        public LevelMediator(
            ILevelDataProxy<MyIdType> dataProxy,
            IDictionary<IWorldVOId, WorldMediator<MyIdType, MyBaseUIElementType>> worldMediators,
            ILevelView<MyIdType, MyBaseUIElementType> view)
            : base(LevelMediatorConstants<MyIdType>.GetMediatorNameFromProxy(dataProxy), view)
        {
            this.proxy = dataProxy;
            this.worldMediators = worldMediators;

            //foreach (IThingDataProxy<MyIdType> itemProxy in proxy.Items.Values)
            //{
            //    ViewComponent.AddUIElement(
            //        itemMediator.GetThingDataProxyView(itemProxy),
            //        itemProxy.Position,
            //        proxy.Map.GetTileHeight(itemProxy.Position).Value);
            //}

            
       }

        public new ILevelView<MyIdType, MyBaseUIElementType> ViewComponent
        {
            get { return (ILevelView<MyIdType, MyBaseUIElementType>)base.ViewComponent; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<String> notifications = base.ListNotificationInterests();
            
            //notifications.Add(proxy.THINGADDED_NOTIFICATION);

            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                default:
                    break;
                    
            }
            base.HandleNotification(notification);
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            List<IWorldView<MyIdType, MyBaseUIElementType>> views = new List<IWorldView<MyIdType, MyBaseUIElementType>>();
            foreach (WorldMediator<MyIdType, MyBaseUIElementType> world in worldMediators.Values)
                views.Add(world.ViewComponent);
            ViewComponent.SetWorldViews(views);

            //ViewComponent.SetMapView(mapMediator.ViewComponent);
            //add dos items existentes?
            //itemMenuMediator.
            //itemMediator.OnPositionChange += new Action<IThingDataProxy<MyIdType>>(itemMediator_OnPositionChange);
            //ViewComponent.MyOnSizeChanged += new Action<Point>(ViewComponent_OnSizeChanged);
            //itemMediator.OnDirectionChange += new Action<IThingDataProxy<ItemVOId>>(itemMediator_OnDirectionChange);
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            //Facade.RemoveMediator(mapMediator.MediatorName);
            //Facade.RemoveMediator(Menu.MediatorName);
           // Facade.RemoveMediator(MenuBar.MediatorName);
        }

        #endregion
    }
}
